package com.lzxuni.modules.pestdetails.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.constant.LogConstant;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.pestdetails.entity.PestCritical;
import com.lzxuni.modules.pestdetails.service.PestCriticalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 虫情管理--虫情状态临界值 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-12-04
 */
@RestController
@RequestMapping("/LR_PestCritical/PestCritical")
public class PestCriticalController extends BaseController{

    @Autowired
    private PestCriticalService pestCriticalService;
    @Autowired
    private FileEntityService fileEntityService;
    private static String typeName = "虫情界限图片";
    private static String ywType = "pest_img";

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/LR_Pest/pestcritical/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        PestCritical pestCritical = JSON.parseObject(queryJson, PestCritical.class);
        PageData pageData = getPageData(pestCriticalService.queryPage(pageParameter, pestCritical));
        return R.ok().put("data", pageData);
    }

    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(String keyValue) throws Exception{
        ModelAndView mv = new ModelAndView("/LR_Pest/pestcritical/form");
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(keyValue);
        fileBeanCustom.setYwType(ywType);
        mv.addObject("imgCount", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        mv.addObject("id", keyValue);
        return mv;
    }
    //保存数据
    @SysLog(categoryId = LogConstant.OPERATEID, module = "虫情预警临界值", operateType = LogConstant.INSERT)
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, PestCritical pestCritica, String pest_imgs) throws Exception{
        pestCritica.setCriticalId(keyValue);
        pestCritica.setCropId(pestCritica.getCropModel());
        if(StringUtils.isEmpty(keyValue)){
            String ywId = UuidUtil.get32UUID();
            if (!com.lzxuni.common.utils.StringUtils.isEmpty(pest_imgs) && !"&amp;nbsp;".equals(pest_imgs)) {
                fileEntityService.insert(pest_imgs.replace("&quot;", "\""), ywId, typeName, ywType, null);
            }
            pestCritica.setCriticalId(ywId);
            pestCritica.setCreateTime(new Date());
            pestCriticalService.save(pestCritica);
            return R.ok("保存成功");
        }else{
            fileEntityService.deleteByYwId(keyValue);
            if (!StringUtils.isEmpty(pest_imgs) && !"&amp;nbsp;".equals(pest_imgs)) {
                fileEntityService.insert(pest_imgs.replace("&quot;", "\""), keyValue, typeName, ywType, null);
            }
            pestCriticalService.updateById(pestCritica);
            return R.ok("修改成功");
        }
    }
    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        fileEntityService.deleteByYwId(keyValue);
        pestCriticalService.removeById(keyValue);
        return R.ok("删除成功");
    }
    //查看
    @RequestMapping("/See")
    public ModelAndView See(String keyValue) throws Exception{
        ModelAndView mv = new ModelAndView("/LR_Pest/pestcritical/see");
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(keyValue);
//        fileBeanCustom.setYwType(ywType);
//        mv.addObject("imgCount", fileEntityService.queryNumByFileEntity(fileBeanCustom));
//        mv.addObject("id", keyValue);
        List<FileEntity> fileEntities = fileEntityService.queryListByFileEntity(fileBeanCustom);
        mv.addObject("fileEntities", fileEntities);
        return mv;
    }

}

